package com.clarte.scheduler;

import java.util.Date;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.quartz.CronTrigger;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SimpleTrigger;
import org.springframework.stereotype.Service;

import com.clarte.model.User;
@Service("schedulerService")
public class SchedulerServiceImpl implements SchedulerService {
	private static Log log = LogFactory.getLog(SchedulerServiceImpl.class);
	@Resource(name="scheduler")
	private Scheduler scheduler;
	
	public SchedulerServiceImpl(){
		
//		User u = new User();
//		u.setUsername("user1");
//		try {
//			scheduleJob(u);
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
	}
	public void tt(){
		log.info("log");
	}
	@Override
	public void scheduleJob(User user) throws Exception {
		log.info("");
		JobDetail jd = new JobDetail();
		jd.setName("job11");
		jd.setJobClass(MyJob.class);
		jd.getJobDataMap().put("user", user);
		
		CronTrigger trigger = new CronTrigger();
		trigger.setName("trigger11");
		trigger.setCronExpression("0/10 * * * * ?");
		trigger.setEndTime(new Date(System.currentTimeMillis()+3*60*1000));
		
		SimpleTrigger strigger = new SimpleTrigger();
		strigger.setName("trigger1");
		strigger.setGroup(Scheduler.DEFAULT_GROUP);
		strigger.setStartTime(new Date());
		strigger.setRepeatInterval(10*1000);
		strigger.setRepeatCount(SimpleTrigger.REPEAT_INDEFINITELY);
		strigger.setEndTime(new Date(System.currentTimeMillis()+3*60*1000));
		
		scheduler.scheduleJob(jd, strigger);
	}

}
